﻿using System;
using System.Reflection;

namespace System.Web.Core
{
    public class BaseSite : ISite
    {
        #region Private Members

        private EntityProvider entityProvider = null;

        #endregion

        #region ISite 成员

        public EntityProvider EntityProvider
        {
            get
            {
                return entityProvider;
            }
            set
            {
                entityProvider = value;
            }
        }

        public virtual void CreateUser(MemberInfo memberInfo)
        {

        }
        public virtual void DeleteUser(Guid memberId)
        {

        }
        public virtual void UpdateUserAttributes(Guid memberId, string attributeName, string attributeValue)
        {

        }

        #endregion

        #region Public Methods

        public TUser GetUser<TUser>(Guid memberId) where TUser : User
        {
            if (memberId == Guid.Empty)
            {
                return null;
            }
            TUser user = null;

            EntityManager.SwitchProvider(EntityProvider);
            try
            {
                user = UserManager.GetUser(memberId) as TUser;
            }
            catch
            { }
            finally
            {
                EntityManager.ResetProvider();
            }

            return user;
        }
        public void CreateUser(User user)
        {
            if (user != null)
            {
                EntityManager.SwitchProvider(EntityProvider);
                try
                {
                    UserManager.CreateUser(user);
                    foreach (Role role in Configuration.Instance.RegisteredDefaultRoleList)
                    {
                        RoleManager.AddUserToRole(user.EntityId.Value, role.EntityId.Value);
                    }
                    user.SetRoles(Configuration.Instance.RegisteredDefaultRoleList);
                }
                catch
                { }
                finally
                {
                    EntityManager.ResetProvider();
                }
            }
        }
        public void DeleteUser<TUser>(Guid memberId) where TUser : User
        {
            TUser user = GetUser<TUser>(memberId);
            if (user != null)
            {
                EntityManager.SwitchProvider(EntityProvider);
                try
                {
                    UserManager.DeleteUser(user.EntityId.Value);
                }
                catch
                { }
                finally
                {
                    EntityManager.ResetProvider();
                }
            }
        }
        public void UpdateUserAttributes(User user, string attributeName, string attributeValue)
        {
            if (user != null)
            {
                EntityManager.SwitchProvider(EntityProvider);
                try
                {
                    bool propertyExist = false;
                    Property property = null;
                    foreach (PropertyInfo propertyInfo in user.GetType().GetProperties())
                    {
                        if (propertyInfo.Name == attributeName)
                        {
                            property = Activator.CreateInstance(propertyInfo.PropertyType) as Property;
                            property.ObjectValue = attributeValue;
                            propertyInfo.SetValue(user, property, null);
                            propertyExist = true;
                            break;
                        }
                    }
                    if (propertyExist)
                    {
                        UserManager.UpdateUser(user);
                    }
                }
                catch
                { }
                finally
                {
                    EntityManager.ResetProvider();
                }
            }
        }
        public void UpdateUser(User user)
        {
            if (user != null)
            {
                EntityManager.SwitchProvider(EntityProvider);
                try
                {
                    UserManager.UpdateUser(user);
                }
                catch
                { }
                finally
                {
                    EntityManager.ResetProvider();
                }
            }
        }

        #endregion
    }
}